package com.hqyj.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.hqyj.mapper.DepartmentMapper;
import com.hqyj.pojo.Department;
import com.hqyj.service.DepartmentService;
import com.hqyj.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DepartmentServiceImpl implements DepartmentService {

    private static final int CACHE_LIFE_TIME = 60*10;

    @Autowired
    private DepartmentMapper departmentMapper;

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public Map<String, Object> selectAll(int pageNum, int pageSize, String departmentName) {
        String key = "dept_page"+pageNum;
        //开启分页
        Map<String, Object> map = new HashMap<>();

        if (!"".equals(departmentName)){
            PageHelper.startPage(pageNum, pageSize);
            List<Department> departments = departmentMapper.selectAll(departmentName);
            PageInfo<Department> departmentPageInfo = new PageInfo<>(departments);
            map.put("code", 200);
            map.put("paging", departmentPageInfo);
        }
        else if (!redisUtil.hasKey(key)){
            PageHelper.startPage(pageNum, pageSize);
            List<Department> departments = departmentMapper.selectAll(departmentName);
            PageInfo<Department> departmentPageInfo = new PageInfo<>(departments);
            map.put("code", 200);
            map.put("paging", departmentPageInfo);
            redisUtil.hmset(key,map,CACHE_LIFE_TIME);
            //System.out.println("查询了数据库");
        }else {
            map = redisUtil.hmget(key);
        }
        return map;
    }

//    @Override
//    public String insertDepartment(Department department) {
//        Integer integer = departmentMapper.insertDepartment(department);
//        if (integer>0){
//            return "200";
//        }else {
//            return "400";
//        }
//    }

    @Override
    public Map<String, Object> inserDepartment(Department department) {
        Integer integer = departmentMapper.insertDepartment(department);
        Map<String, Object> map = new HashMap<>();
        if (integer>0){
            delCache();
            map.put("code",200);
            return map;
        }else {
            map.put("code",400);
            map.put("message","失败");
            return map;
        }
    }
    //删除
    @Override
    public Map<String, Object> deleteDepartment(String depatmentName) {
        Integer integer = departmentMapper.deleteDepartment(depatmentName);
        HashMap<String, Object> map = new HashMap<>();
        if (integer>0){
            delCache();
            map.put("code",200);
            return map;
        }else {
            map.put("code",400);
            return map;
        }
    }

    @Override
    public Map<String, Object> updateDepartment(Department department) {
        Integer integer = departmentMapper.updateDepartment(department);
        HashMap<String, Object> map = new HashMap<>();
        if (integer>0){
            delCache();
            map.put("code",200);
            return map;
        }else {
            map.put("code",500);
            return map;
        }
    }

    public void delCache(){
        redisUtil.del(redisUtil.keysLike("dept_page"));
    }

    //获取所有部门
    @Override
    public Map<String, Object> getAll() {
        List<Department> departmentList = departmentMapper.getAll();
        HashMap<String, Object> map = new HashMap<>();
        if (departmentList!=null){
            map.put("code",200);
            map.put("departmentList",departmentList);
            return map;
        }else {
            map.put("code",500);
            return map;
        }
    }
}
